﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.Practices.ServiceLocation;
using YoProgramo.Domain.BusinessLogic.Service;
using YoProgramo.Domain.Core;
using YoProgramo.Domain.Core.Entities;
using YoProgramo.Domain.Core.Interfaces;

namespace YoProgramo.Domain.BusinessLogic.Extensions
{
    public static class UserExtension {
        public static bool IsInRole(this IUser user, string rol)
        {
            Roles r = (Roles)Enum.Parse(typeof(Roles), rol, true);
            if (IsInRole(user, r))
                return true;

            return false;           
        }

        public static bool IsInRole(this IUser user, Roles rol) {
            if(user != null) {
                if (user.Role == rol)
                    return true;
            }

            return false;
        }

        public static void UpdateLastActivityDate(this IUser user) {
            if (user != null) {
                var userservice = ServiceLocator.Current.GetInstance<IUserService>();
                user.LastActivityAt = DateTime.Now;
                userservice.Update(user);
            }
        }

        public static bool IsSpammer(this IUser user) {
            
            var StorySpamService = ServiceLocator.Current.GetInstance<IMarkAsSpamService<Story>>();

            if (StorySpamService.IsSpammer(user)) return true;

            var CommentSpamService = ServiceLocator.Current.GetInstance<IMarkAsSpamService<Comment>>();

            if (CommentSpamService.IsSpammer(user)) return true;

            return false;
        }


        public static double GetReputation(this IUser user)
        {
            var voteCommentService = ServiceLocator.Current.GetInstance<IVoteService<Comment>>();
            var commentvotes = voteCommentService
                                    .GetVotesByUser(user)
                                        .Where(w => ((Comment)w.VoteFor).ForStory.IsLockedOut != true);

            var storyService = ServiceLocator.Current.GetInstance<IStoryService>();
            var commentService = ServiceLocator.Current.GetInstance<ICommentService>();

            int userCommentsCount = commentService.GetByUser(user).Count;
            int userStoriesCount = storyService.GetByUser(user).Count;

            int votesNegative = (int)commentvotes.Where(x => x.Rate == -1).Count();
            int votePositive = (int)commentvotes.Where(x => x.Rate == 1).Count();

            TimeSpan tsSinceFromRegistered = DateTime.Now.Subtract(user.RegisteredAt);
            int daysSinceFromRegistered = tsSinceFromRegistered.Days;
            if (daysSinceFromRegistered == 0) daysSinceFromRegistered = 1;

            TimeSpan tsInactive = DateTime.Now.Subtract(user.LastActivityAt);
            int daysInactive = tsInactive.Days;
            if (daysInactive == 0) daysInactive = 1;

            double netScore = (double) daysSinceFromRegistered / (daysInactive * 2);

            double reputation = (double)((votePositive * netScore) - votesNegative) + userCommentsCount + userStoriesCount;

            return reputation;
        }

        public static void ToEnableCheck(this IUser user) {
            if (user.IsLockedOut) HttpContext.Current.Response.Redirect("/Error/KickBan");
        }

    }
}